import { axiosInstance } from 'src/boot/axios'
import CommonConfig from 'src/assets/js/config'
import qs from 'qs'
import moment from 'moment'
const baseUrl = CommonConfig.messagestationUrl + '/v1'
const schemaUrl = CommonConfig.schemaUrl + '/v1'

const qsConfig = {
  arrayFormat: 'repeat',
  skipNulls: true,
  format: 'RFC3986'
}
// 获取生产组织架构-树形选择器结构
function getOrganize () {
  const newUrl = schemaUrl + '/scheme_tree'

  return axiosInstance.get(newUrl).then((res) => {
    return Promise.resolve(res)
  })
}
// 回显车间id
function getSelectedList (userId) {
  const newUrl = baseUrl + `/message/workshop/list/${userId}`
  return axiosInstance.get(newUrl).then((res) => {
    return Promise.resolve((res))
  })
}
// 查询车间id
function getWorkshopList (userId) {
  const newUrl = schemaUrl + '/workshop_kv'
  return axiosInstance.get(newUrl).then((res) => {
    return Promise.resolve((res))
  })
}
// PUT
// /v1/message/workshop/bind/{userId}
// 绑定车间id
function putWorkshopBind (userId, { workShopIds, plantIds }) {
  const url = baseUrl + `/message/workshop/bind/${userId}`
  const params = {
    workShopIds,
    plantIds
  }
  return axiosInstance.put(url, params).then((res) => {
    return Promise.resolve((res))
  })
}
// /v1/message/center/read/{userId}
// 返回传入业务分类的 未读个数
function getMsgRead (userId) {
  const newUrl = baseUrl + `/message/center/read/${userId}`
  const params = {
    system: 0
  }
  return axiosInstance.get(newUrl, { params }).then((res) => {
    return Promise.resolve(res)
  })
}
// GET
// /v1/message/center/page
// 根据业务类型查询消息分页
function getMsgList ({
  current, // 当前页码(从1计
  label, // 业务分类
  size, // 一页数量,
  read
}) {
  const newUrl = baseUrl + `/message/center/page`
  const params = {
    system: 0,
    current,
    label,
    read, // 过滤条件(0:未读 1:已读 2:全部) dlp 为全部
    size,
    time: moment(new Date()).format('YYYY-MM-DD HH:mm:ss')
  }
  const config = {
    params,
    paramsSerializer: function (ids) {
      return qs.stringify(ids, qsConfig)
    }
  }
  return axiosInstance.get(newUrl, config).then((res) => {
    return Promise.resolve(res)
  })
}
// GET
// /v1/message/center/platform/detail/{messageId}
// 查看详情并设置已读
function getMsgPlatformDetail (messageId) {
  const newUrl = baseUrl + `/message/center/platform/detail/${messageId}`
  return axiosInstance.get(newUrl).then((res) => {
    return Promise.resolve(res)
  })
}
// DELETE
// /v1/message/center/delete/{userId}/dlp
// 消息删除(dlp)
function deleteMsg (userId, { label }) {
  const newUrl = baseUrl + `/message/center/delete/${userId}/dlp`
  const config = {
    params: {
      label
    },
    paramsSerializer: function (ids) {
      return qs.stringify(ids, qsConfig)
    }
  }
  return axiosInstance.delete(newUrl, config).then((res) => {
    return Promise.resolve(res)
  })
}
// PUT
// /v1/message/center/init/read/{userId}
// 初始化对应业务的已读位置
function putInitRead (userId, { label }) {
  const system = 0
  const url = baseUrl + `/message/center/init/read/${system}/${userId}`
  return axiosInstance.put(url, label).then((res) => {
    return Promise.resolve((res))
  })
}

// /v1/message/center/read/{userId}
// 返回传入业务分类的 未读个数
function getMsgUnRead (userId) {
  const system = 0
  const newUrl = baseUrl + `/message/center/read/${userId}?system=${system}`
  return axiosInstance.get(newUrl).then((res) => {
    return Promise.resolve(res)
  })
}

/**
 * 批量删除消息
 * @param userId
 * @param messages
 * @returns {Promise<AxiosResponse<any>>}
 */
export function deleteMsgBySel (userId, messages) {
  const newUrl = baseUrl + `/message/center/delete/${userId}/platform`
  return axiosInstance.delete(newUrl, { data: messages }).then((res) => {
    return Promise.resolve(res)
  })
}

/**
 * 标记消息已读
 * @param messageIds
 */
export function readMsgBatch (messageIds) {
  const newUrl = baseUrl + `/message/center/read_batch`
  const config = {
    params: {
      messageIds,
      system: 0
    },
    paramsSerializer: function (ids) {
      return qs.stringify(ids, qsConfig)
    }
  }
  return axiosInstance.put(newUrl, null, config).then((res) => {
    return Promise.resolve(res)
  })
}

function getUnreadSum () {
  const system = 0
  const newUrl = `${baseUrl}/message/center/total/${system}`

  return axiosInstance.get(newUrl).then((res) => {
    return Promise.resolve(res)
  })
}
export default {
  getOrganize,
  getWorkshopList,
  putWorkshopBind,
  getMsgRead,
  getMsgList,
  getMsgPlatformDetail,
  deleteMsg,
  putInitRead,
  getMsgUnRead,
  getSelectedList,
  getUnreadSum
}
